home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / Apps / DevTools / eText5 / Source / NewXTeXT / XText0.9beta2 / XTDemo.m < prev    next >
Encoding:
Text File  |  1995-07-28  |  2.9 KB  |  143 lines

  1. #import "XTDemo.h"
  2.  
  3. @implementation XTDemo
  4. - init
  5. {
  6.     [super init];
  7.     demoAction = [[XTDispatchAction alloc] init];
  8.     fieldAction = [[XTDispatchAction alloc] init];
  9.     [fieldAction addBindings:"cs'Q=insertKeyCombOfNextKey" estream:nil];
  10.     return self;
  11. }
  12.  
  13. - appDidInit:sender
  14. {
  15.     id my_font = [Font newFont:"Ohlfs" size:14.0];
  16.     
  17.     demoText = [demoText docView];
  18.     [demoText setFont:my_font];
  19.     [demoText setInitialAction:demoAction];
  20.     [demoText setSel:0 :0];
  21.     [[demoText window] makeKeyAndOrderFront:self];
  22.     return self;
  23. }
  24.  
  25. - windowWillReturnFieldEditor:sender toObject:client
  26. {    
  27.     id field_font = [Font newFont:"Ohlfs" size:12.0];
  28.  
  29.     fieldText = [XText newFieldEditorFor:sender
  30.                     initialAction:fieldAction
  31.                     estream:nil];
  32.  
  33.     [fieldText setFont:field_font];
  34.  
  35.     return fieldText;
  36. }    
  37.  
  38. - applyBinding:sender
  39. {
  40.     [demoAction addBindings: [cmdField stringValue]
  41.         estream:[demoText errorStream]];
  42.     return self;
  43. }
  44.  
  45. - insertKey:sender
  46. {
  47.     [fieldText setSel:0 :0];
  48.     [fieldText insertKeyCombOfNextKey];    
  49.  
  50.     return self;
  51. }
  52.  
  53. - clearBindings:sender
  54. {
  55.     id newAction;
  56.                 
  57.     /* if anyone thinks this will cause a memory leak, let me know P.G. */
  58.     newAction = [[XTDispatchAction alloc] init];   
  59.     [demoText setInitialAction:newAction];                
  60.     [demoAction free];
  61.     demoAction = newAction;
  62.     
  63.     return self;
  64.  
  65. }
  66.  
  67. - openFile:sender
  68. {
  69.   const char *file;
  70.   char path[MAXPATHLEN +1];
  71.   id openPanel;  
  72.  
  73.   [[NXBundle mainBundle] getPath:path forResource:"KeyBindingFiles" ofType:""];
  74.  
  75.   openPanel = 
  76.       [[[OpenPanel new] allowMultipleFiles:NO] setTitle:"Keybinding File"];
  77.  
  78.     /* run the open panel */
  79.     if ([openPanel runModalForDirectory:path file:""]){
  80.  
  81.         /* open the file returned by the save panel */
  82.         file = [openPanel filename];
  83.         strcpy(fullName, file);
  84.         [loadButton setEnabled:YES];    
  85.         sprintf(titleString, "Keybinding File: %s", *[openPanel filenames]);
  86.            [[demoText window] setTitle:titleString];
  87.  
  88.         /* open file from the workspace-->default editor) */
  89.         [[Application workspace] openFile:file];
  90.     }
  91.     
  92.     return self;
  93. }
  94.   
  95. - saveBinding:sender
  96. {
  97.     FILE *fp;
  98.     const char *file;
  99.     id savePanel;  
  100.     
  101.     if(![loadButton isEnabled]) {
  102.         savePanel = [[SavePanel new] setTitle:"Keybinding File"];
  103.         if ([savePanel runModal]) {
  104.  
  105.             /* use the file returned by the save panel */
  106.             file = [savePanel filename];
  107.             strcpy(fullName, file);
  108.             sprintf(titleString, "Keybinding File: %s", [savePanel filename]);
  109.                [[demoText window] setTitle:titleString];
  110.             [loadButton setEnabled:YES];
  111.         }
  112.         else return self;
  113.     }
  114.     
  115.     fp = fopen(fullName, "a+"); /* append */
  116.     
  117.     if(!fp){
  118.         NXRunAlertPanel("Save Binding","Can't Append to %s", "Okay", 
  119.         NULL, NULL, fullName);
  120.         return self;
  121.     }
  122.     
  123.     /* write the comment (rather primitively)*/
  124.     fprintf(fp,"%s","#");
  125.     fprintf(fp,"%s",[comField stringValue]);
  126.     fprintf(fp,"\n");
  127.  
  128.     /* write the binding */
  129.     fprintf(fp,"%s",[cmdField stringValue]);
  130.     fprintf(fp,"\n");
  131.     
  132.     fclose(fp);
  133.     
  134.     return self;
  135. }    
  136.  
  137. - loadBindings:sender
  138. {        
  139.     [demoAction loadFromFile:fullName estream:nil];        
  140.     return self;
  141. }
  142. @end
  143.